/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.r3000;

import java.sql.*;

import nfse.NetFSE;
import nfse.NetFSERecord;
import nfse.NetFSEUtil;
import nfse.gui.record.FlexRecord;
import nfse.gui.record.FlexRecordField;

/*******************************************************************************
 * This class extends the NetFSERecord class to parse and store R3000 web logs.
 * 
 * @author uphoff
 * 
 */
public class R3000Record extends NetFSERecord {


    /***************************************************************************
     * The type of request
     */
    public String type = "";

    /***************************************************************************
     * The category of the request (e.g. SPORTS)
     */
    public String category = "";

    /***************************************************************************
     * The web site visited
     */
    public String url = "";

    public String urlBase = "";

    public static void main(String[] args) {
        R3000Record rec = new R3000Record();
        rec.parse(args, rec);
    }
    
    public String get(int field) {
        String temp = "";
        int len = 0;
        switch (field) {
        case (NetFSE.FIELD_START_TS):
            temp = Long.toHexString(this.getTimestamp().getTime() / 1000);
            len = 8;
            break;
        case (NetFSE.FIELD_SRC_IP):
            temp = Long.toHexString(NetFSEUtil.convertIPtoLong(this.getSrcAddr()));
            len = 8;
            break;
        default:
            temp = null;
            break;
        }
        if (temp != null) {
            while (temp.length() < len)
                temp = "0" + temp;
        }
        return temp;
    }

    public FlexRecord getFlexRecord() {
        FlexRecord temp = new FlexRecord();
        temp.values = new String[6];
        int idx = -1;
        temp.values[++idx] = this.getTimestamp().toString();
        temp.values[++idx] = this.getSrcAddr();
        temp.values[++idx] = this.type;
        temp.values[++idx] = this.category;
        temp.values[++idx] = this.urlBase;
        temp.values[++idx] = this.url;
        return temp;
    }

    public FlexRecordField[] getFlexRecordFields() {
        FlexRecordField[] temp = new FlexRecordField[6];
        int idx = -1;
        temp[++idx] = new FlexRecordField("Start Time", "startts", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_TIMESTAMP);
        temp[++idx] = new FlexRecordField("Src IP", "srcip", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_IP_ADDR);
        temp[++idx] = new FlexRecordField("Type", "type", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_EMAIL_ADDR);
        temp[++idx] = new FlexRecordField("Category", "category", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_EMAIL_ADDR);
        temp[++idx] = new FlexRecordField("URL Base", "urlbase", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("URL", "url", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_NONE);
        return temp;
    }

    /***************************************************************************
     * Parse a comma-delimited R3000 record into a R3000Record data structure.
     * 
     * @param line
     *            The tab-delimitted R3000 record
     * @return The parsed R3000 record data structure
     */
    public NetFSERecord generate(String line, int sensor) {
        try {
            R3000Record temp = new R3000Record();
            line = line.replaceAll("'"," ");
            if (line.endsWith("\\")) {
                line = line.substring(0,line.length()-1);
            }
            temp.setInitialString(line); 
            temp.setTypeID(typeID);
            temp.setTypeName(typeName);
            String[] values = line.split(",");
            String date = values[2].replaceAll("/", "-");
            temp.setStartTS(Timestamp.valueOf(date + " " + values[3]));
            temp.setSrcIP(NetFSEUtil.convertIPtoLong(values[0]));
            temp.category = values[1];
            temp.type = values[4];
            for (int i = 5; i < values.length; i++) {
                if (i > 5)
                    temp.url += ",";
                temp.url += values[i];
            }
            temp.setSensorID(sensor);
            return temp;
        } catch (Exception E) {
            E.printStackTrace();
            System.out.println("R3000, BAD RECORD: '" + line + "'");
            return null;
        }
    }

    public String getCreateFieldTableSQL(String table) {
        StringBuffer temp = new StringBuffer("create table " + table + " (ID bigint, ");
        temp.append("srcip inet,");
        temp.append("category varchar(20),");
        temp.append("type varchar(20),");
        temp.append("urlbase varchar(255),");
        temp.append("url text,");
        temp.append("startts timestamp");
        temp.append(")");
        return temp.toString();
    }

    public String getInsertFieldTableSQL(int id, String table) {
        StringBuffer temp = new StringBuffer("insert into " + table + " values (" + id + ",'");
        temp.append(this.getSrcAddr() + "',");
        temp.append("'" + category + "',");
        temp.append("'" + type + "',");
        temp.append("'" + urlBase + "',");
        temp.append("'" + url + "',");
        temp.append("'" + this.getTimestamp() + "'");
        temp.append(")");
        return temp.toString();
    }
    
    

}